$(document).ready(function(){
	initGoods();
});



//获取商品列表


function showGoods(goodsList){
	for (var i=0;i<goodsList.length;i++) {
		$("#goodsList").append('<tr id=goods_'+goodsList[i].id+'><td class="goods_img"><img src="..'+goodsList[i].path+'"/></td><td class="goods_name">'+goodsList[i].name+'</td><td class="goods_cost">'+goodsList[i].cost+'</td><td class="goods_stock">'+goodsList[i].stock+'</td><td><a href="modifyInfo.html?id='+goodsList[i].id+'"><button onclick="updateGoods(this)">修 改</button></a><button class="del" onclick="delGoods(this)">删 除</button></td></tr>');
	}
}


function initGoods(){
	var url = "/HyperIT/goods/findGoods";
	$.get(url,function(data){
		if(data.serviceResult){
			var goodsList = data.resultParm.goodslist;
			showGoods(goodsList);
		}
		else{
			alert(data.resultInfo);
		}
	});
}



//删除商品

var $goodsTr
function delGoods (which) {
	$goodsTr = $(which).parent().parent();
	var msg = "确定要删除该商品吗？";
		if (confirm(msg)==true){
			var id = $goodsTr.attr("id").split("_")[1];
			var url = "/HyperIT/goods/deleteGoods";
			var parm = "id="+id;
			$.post(url,parm,function(data){
				if(data.serviceResult){
					$goodsTr.remove();	
				}
			});
		}
		else{
				return false;
			}
	}

//新建商品，上传图片、名字、价格、库存

function newGoods() {

	var name = $("#name").val();
	var cost = $("#range1").val();
	var stock = $("#range2").val();
	var formData = new FormData($("#good_picture")[0]);
	formData.append("name", name);
	formData.append("cost", cost);
	formData.append("stock",stock);
	$.ajax({
		url: '/HyperIT/goods/saveGoods',
		type: 'POST',
		data: formData,
		async: false,
		cache: false,
		contentType: false,
		processData: false,
		success: function(data) {
			if(data.serviceResult){
				alert("新建成功");
				window.location="goodsList.html";
				
			}else{
				alert(data.resultInfo);
			}
		},
		error: function(data) {
			alert("提交失败！");
		}
	});
}


//上传图片将图片在固定位置展示


$(function() {
	$("#file_upload").change(function() {
		var $file = $(this);
		var fileObj = $file[0];
		var windowURL = window.URL || window.webkitURL;
		var dataURL;
		var $img = $("#preview");

		if(fileObj && fileObj.files && fileObj.files[0]) {
			dataURL = windowURL.createObjectURL(fileObj.files[0]);
			$img.attr('src', dataURL);
		} else {
			dataURL = $file.val();
			var imgObj = document.getElementById("preview");
			// 两个坑:
			// 1、在设置filter属性时，元素必须已经存在在DOM树中，动态创建的Node，也需要在设置属性前加入到DOM中，先设置属性在加入，无效；
			// 2、src属性需要像下面的方式添加，上面的两种方式添加，无效；
			imgObj.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale)";
			imgObj.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = dataURL;

		}
	});
});


//修改商品信息

//var $thisGoods;
//function updateGoods(which) {
//	window.location="modifyInfo.html";
//	$thisGoods = $(which).parent().parent();
//	var id = $thisGoods.attr("id").split("_")[1];
//	var name = $thisGoods.children(".goods_name").val();
//	$.get(url,parm,function(data){
//		if(data.serviceResult){
//			
//		}
//		else{
//			alert(data.resultInfo);
//		}
//	});
//	
//}
